// let testEditor=document.getElementById("test-editor");
let textarea = document.getElementsByTagName('textarea')[1]
// let D=document.getElementsByClassName("D")[0];
// /*输入框显示时间为默认的文章描述*/
let InputValueTime=document.getElementsByClassName("InputValueTime")[0];

/*文章标签*/
let labelNumber=0;
let tenLabel=document.getElementsByClassName("tenLabel");
let array=new Array(10);
for(let i=0;i<10;i++){
    array[i]=0;
}
function TYPE(i,s){
    click();
    function click(){
        tenLabel[i].onclick=function(){
            tenLabel[i].style.backgroundColor="#42bbfd";
            tenLabel[i].style.color="#ffffff";
            labelNumber++;
            /*点击那个数组相应位置为1，表示添加*/
            array[i]=s;
            tenLabel[i].onclick=function(){
                tenLabel[i].style.backgroundColor="";
                tenLabel[i].style.color="";
                labelNumber--;
                /*点击那个数组相应位置为1，表示删除*/
                array[i]=0;
                click();
            }
        }
    }
}
TYPE(0,"生");
TYPE(1,"二");
TYPE(2,"音");
TYPE(3,"美");
TYPE(4,"影");
TYPE(5,"运");
TYPE(6,"科");
TYPE(7,"技");
TYPE(8,"社");
TYPE(9,"其");

let tenLabelT=document.getElementsByClassName("tenLabelT");
let labelNumberT=0;
for(let i=0;i<tenLabelT.length;i++) {
    clickT();

    function clickT() {
        tenLabelT[0].onclick = function () {
            tenLabelT[0].style.backgroundColor = "#42bbfd";
            tenLabelT[0].style.color = "#ffffff";
            tenLabelT[1].style.backgroundColor = "";
            tenLabelT[1].style.color = "";
            labelNumberT = 1;
            /*点击那个数组相应位置为1，表示添加*/
        }
        tenLabelT[1].onclick = function () {
            tenLabelT[1].style.backgroundColor = "#42bbfd";
            tenLabelT[1].style.color = "#ffffff";
            tenLabelT[0].style.backgroundColor = "";
            tenLabelT[0].style.color = "";
            labelNumberT = 2;
        }
    }
}
    /*昵称*/
    let messageNameS = document.getElementsByClassName("messageNameS")[0];
    /*发布文章按钮*/
    let releaseArticle = document.getElementsByClassName("releaseArticle")[0];
    let titleLength = document.getElementsByClassName("titleLength")[0];
    /*点击判断输入框长度*/
    releaseArticle.onclick = function () {
        if (InputValueTime.value.length < 5 || InputValueTime.value.length > 60) {
            set("标题字数应在5-60之间");
        } else if (labelNumber == 0) {
            set("请选择文章标签");
        } else if (labelNumberT == 0) {
            set("请选择文章类型");
        } else {
            /*这里是ajax点击发送请求*/
            /*文章内容textarea.innerText*/
            /*array()文章标签*/
            /*arrayT文章类型*/
            /*-其他数据可以从本地获取*/
            // console.log(textarea.innerText)
            $.ajax({
                url: "../blog/releaseBlog",
                type: "post",
                data: {
                    blogger: sessionStorage.getItem("nickname"),
                    content: textarea.innerText,
                    toc: labelNumberT,
                    type: array,
                    title: InputValueTime.value
                },
                success: function (data) {
                    set("发布成功");
                    setTimeout(function(){
                        window.onbeforeunload = function (event) {

                        }
                        setTimeout(function(){
                            window.location.replace('../blogs/findBlogsByBlogger');
                            setTimeout(function(){
                                window.history.back(-1);
                            },5000)
                        },100)

                    },1000)
                }
            });
        }
    }

    function set(i) {
        titleLength.innerHTML = i;
        titleLength.style.bottom = "56px";
        setTimeout(function () {
            titleLength.style.bottom = "-56px";
        }, 1000)
    }
window.onbeforeunload = function (event) {
    var event = event || window.event;
// 兼容IE8和Firefox 4之前的版本
    if (event) {
        event.returnValue = "确定要关闭窗口吗？";
    }
// Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
    return '确定要关闭窗口吗>现代浏览器？';
}